package com.tf_cut.backend.platform.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.google.common.collect.Lists;
import com.tf_cut.backend.agent.service.AgentAccountService;
import com.tf_cut.backend.agent.service.AgentCashOrderService;
import com.tf_cut.backend.agent.vo.AgentCashOrderVo;
import com.tf_cut.backend.common.pojo.PageInfo;
import com.tf_cut.backend.common.pojo.Result;
import com.tf_cut.backend.payment.service.PaymentService;
import com.tf_cut.backend.util.ConfigSettingUtil;
import com.tf_cut.backend.util.OrderPaymentQueue;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/backend/admin/platform/agent/withdraw")
public class AgentWithdrawController {
    private Logger logger = LogManager.getLogger(AgentWithdrawController.class);

    @Resource
    private ConfigSettingUtil configUtil;
    @Autowired
    private AgentCashOrderService agentCashOrderService;
    @Autowired
    private AgentAccountService agentAccountService;
    @Autowired
    private PaymentService paymentService;

    @PostMapping("/list")
    public String queryPlatformAgentWithdrawList(String json) {
        Result<List<AgentCashOrderVo>> result;
        try {
            AgentCashOrderVo avo = JSON.parseObject(json, AgentCashOrderVo.class);
            PageInfo<AgentCashOrderVo> list = agentCashOrderService.findListAgentCashOrder(avo);
            return JSON.toJSONString(list,SerializerFeature.WriteDateUseDateFormat);
        } catch (Exception e) {
            log.error("queryPlatformAgentWithdrawList error:{}", e.getMessage(), e);
            result = Result.of(Lists.newArrayList());
            result.setMsg(e.getMessage());
            result.setFlag(false);
        }
        return JSON.toJSONString(result, SerializerFeature.WriteDateUseDateFormat);
    }

    @PostMapping("/update")
    public String updateAgentCashOrder(@RequestBody String json) {
        Result<String> result = Result.of("更新成功");;
        try {
            AgentCashOrderVo agentCashOrderVo = JSON.parseObject(json, AgentCashOrderVo.class);
            Result<AgentCashOrderVo> r = agentCashOrderService.get(agentCashOrderVo.getOrderId());
            AgentCashOrderVo agentCashOrderVo1 = r.getData();
            agentCashOrderVo1.setOrderType(agentCashOrderVo.getOrderType());
//            agentCashOrderService.updateAgentCashOrder(agentCashOrderVo1);

            if(agentCashOrderVo.getOrderType() == 1) {
                String isPayment = configUtil.getConfigValue("payment","is_payment");    //支付开关

                if(isPayment.equals("off")){
                    agentCashOrderVo1.setPaymentStatus("N");
                }else if(isPayment.equals("on")){
                    agentCashOrderVo1.setPaymentStatus("O");
                    //如果是线上支付，将该发放订单加入支付队列中
                    logger.info("提现订单："+agentCashOrderVo1.getOrderId()+"当前线上支付，加入支付队列等待支付.");
                    //OrderPaymentQueue.set(agentCashOrderVo1);
                }

                /*
				//使用支付队列自动发起支付
                int res = paymentService.paymentAgentCash(agentCashOrderVo1);

                if (res == 0) {
                    result.setMsg("提交银行处理中...");
                    result.setFlag(true);
                } else if (res == 1) {
                    result = Result.of("更新失败");
                    result.setMsg("提交银行处理超时.");
                    result.setFlag(false);
                } else if(res == 9){
                    //没有开始支付
                }
                 */
            }
            result.setMsg("提交银行处理中...");
            result.setFlag(true);
            agentCashOrderService.updateAgentCashOrder(agentCashOrderVo1);
        } catch (Exception e) {
            log.error("updateAgentCashOrder reqParam={},error:{}",json, e.getMessage(), e);
            result = Result.of("更新失败");
            result.setMsg(e.getMessage());
            result.setFlag(false);
        }
        return JSON.toJSONString(result, SerializerFeature.WriteDateUseDateFormat);
    }

}
